using System;
using Habanero.Base;
using Habanero.BO;
using Habanero.BO.ClassDefinition;
using Habanero.UI.Base;
using Habanero.UI.Win;
using UseCaseManager.BO;

namespace UseCaseManager.UI
{
    /// <summary>
    /// A parent-child grid control used for adding and editing Projects and their Packages.
    /// </summary>
    public class ProjectsAndPackagesControl : UserControlWin, IFormControl
    {
        private readonly IControlFactory _controlFactory;

        public ProjectsAndPackagesControl(IControlFactory controlFactory)
        {
            _controlFactory = controlFactory;

            IEditableGridControl projectsGridControl = SetupParentControl();
            IEditableGridControl packagesGridControl = SetupChildControl();

            // Add the grids to the control. The Projects grid fills the top part of the control.  
            // Change the size here depending on how many items you want to see in this grid.  
            // The Packages grid fills the rest of the control.
            BorderLayoutManager layoutManager = _controlFactory.CreateBorderLayoutManager(this);
            projectsGridControl.Height = 200;
            layoutManager.AddControl(packagesGridControl, BorderLayoutManager.Position.Centre);
            layoutManager.AddControl(projectsGridControl, BorderLayoutManager.Position.North, true);

            // Connect the Projects and Packages grids so that when an item is selected
            // in the Projects grid, the correct Packages are shown in the child grid.
            projectsGridControl.Grid.BusinessObjectSelected += 
                delegate
                    {
                        try
                        {
                            Project selectedProject = projectsGridControl.Grid.SelectedBusinessObject as Project;
                            if (selectedProject == null) return;
                            packagesGridControl.BusinessObjectCollection = selectedProject.Packages;
                        }
                        catch (Exception ex)
                        {
                            GlobalRegistry.UIExceptionNotifier.Notify(ex, "", "Error ");
                        }
                    };
        }

        private IEditableGridControl SetupParentControl()
        {
            // Create the Projects read-only grid
            IEditableGridControl projectsGridControl = _controlFactory.CreateEditableGridControl();

            // Used to specify a custom UI def
            projectsGridControl.Initialise(ClassDef.Get<Project>(), "default");

            // Load the collection of Projects.  You can set the criteria and order by values here.
            BusinessObjectCollection<Project> projectsCollection = Broker.GetBusinessObjectCollection<Project>("", "");
            projectsGridControl.BusinessObjectCollection=projectsCollection;

            // Set this to true to enable the filters, then add your filters below (see commented out line)
            projectsGridControl.FilterControl.Visible = false;
            //projectsGridControl.FilterControl.AddStringFilterTextBox("", "");

            // Uncomment this line to enable the default delete button
            //projectsGridControl.Buttons.ShowDefaultDeleteButton = true;

            return projectsGridControl;
        }

        private IEditableGridControl SetupChildControl()
        {
            // Create the Packages read-only grid to show the child items
            IEditableGridControl packagesGridControl = _controlFactory.CreateEditableGridControl();

            // Used to specify a custom UI def
            packagesGridControl.Initialise(ClassDef.Get<Package>(), "default");

            // Creates an empty collection of Packages to put in the child grid
            BusinessObjectCollection<Package> t2Collection = new BusinessObjectCollection<Package>();
            packagesGridControl.BusinessObjectCollection=t2Collection;

            // Set this to true to enable the filters, then add your filters below (see commented out line)
            packagesGridControl.FilterControl.Visible = false;
            //packagesGridControl.FilterControl.AddStringFilterTextBox("", "");

            // Uncomment this line to enable the default delete button
            //packagesGridControl.Buttons.ShowDefaultDeleteButton = true;

            return packagesGridControl;
        }

        /// <summary>
        /// Called whenever the control is placed on a form by the menu system.  Put any initialisation
        /// code in here that must be run whenever the form is displayed.
        /// </summary>
        public void SetForm(IFormHabanero form) { }
    }
}
